<template>
  <h1>登录</h1>
  <el-card style="width: 450px;margin: 0 auto;padding: 20px 10px 0 0">
    <el-form label-width="60px">
      <el-form-item label="用户名:" >
        <el-input placeholder="请输入用户名" v-model="user.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" >
        <el-input placeholder="请输入密码" type="password" v-model="user.password"></el-input>
      </el-form-item>
      <el-form-item >
        <el-button type="primary" @click="login()">登录</el-button>
      </el-form-item>

    </el-form>
  </el-card>
</template>

<script setup>
import {ref} from "vue";
import axios from "axios";
import{ElMessage} from "element-plus";
import router from "@/router";
import qs from "qs";

const user=ref({
  username:'',
  password:''
})
const login=()=>{
  console.log(user.value);
  //把JSON字符串转换格式为&连接的字符串
  let data=qs.stringify(user.value);

  axios.post("http://localhost:8080/v1/users/login",data)
        .then(
            (response)=>{
              if (response.data.code==2001){
                let user=response.data.data;
                console.log(user);
                ElMessage.success('欢迎回来'+user.nickname);
                localStorage.setItem('user',JSON.stringify(user));
                router.push('/');
              }else {
                ElMessage.warning(response.data.msg);
              }

            }
        )
}

</script>

<style scoped>

</style>